/*    
 * THis file is part of the xoperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.sf.net
 * Copyright (C) 2007  Jörg Unbehauen  

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package org.aksw.xoperator.sparql.p2p;

import org.aksw.xoperator.Constants;
import org.aksw.xoperator.sparql.ISparqlEndpoint;
import org.aksw.xoperator.sparql.SparqlEndpointIdentifier;
import org.aksw.xoperator.sparql.SparqlEndpointType;
import org.aksw.xoperator.sparql.SparqlResult;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.dom2_builder.DOM2XmlPullBuilder;


public class SPARQLResultoverXmpp extends IQ {

	private String result;

	private String storeName;
	
	private boolean sent;
	
	public void setSent(boolean sent) {
		this.sent = sent;
	}
	
	public boolean isSent() {
		return sent;
	}
	

	public String getResult() {
		return this.result;
	}

	public String getStoreName() {
		return this.storeName;
	}
	
	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}
	
	public SPARQLResultoverXmpp(SparqlResult result){
		this.result = result.getAnswer();
		this.storeName = result.getAnsweredBy().getName();
	}


	public SPARQLResultoverXmpp(String result, String storeName) {
		super();
		this.result = result;
		this.storeName = storeName;
		this.setType(IQ.Type.RESULT);
	}
	
	public SPARQLResultoverXmpp(String storeName, XMPPError.Condition condition, String errorMessage) {
		super();
		this.storeName = storeName;
		this.setError(new XMPPError(XMPPError.Condition.forbidden,errorMessage));
		this.setType(IQ.Type.ERROR);
	}
	
	public SPARQLResultoverXmpp(String result) {
		super();
		this.result = result;

		this.setType(IQ.Type.RESULT);
	}

	@Override
	public String getChildElementXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<sparql-result xmlns=\"");
		buffer.append(Constants.SPARQL_XMPP_SPEC_URL);
		buffer.append("\">");
		buffer.append("<meta>");
		buffer.append("<storename>");
		buffer.append(storeName);
		buffer.append("</storename>");
		buffer.append("</meta>");
		buffer.append(clearResult(result));
		
		buffer.append("</sparql-result>");

		return buffer.toString();
	}
	
	
	private static String clearResult(String result){
		return result.substring(result.indexOf("<sparql"));
	}
	
	
	public static class Provider implements IQProvider{
		
		public IQ parseIQ(XmlPullParser xpp) throws Exception {
			String meta = "";
			String result = "";
			
			Element ele = new DOMBuilder().build(new DOM2XmlPullBuilder().parseSubTree(xpp));

			Namespace ns = Namespace.getNamespace(Constants.SPARQL_XMPP_SPEC_URL);

			meta = ele.getChild("meta",ns).getChild("storename",ns).getText();

			result = new XMLOutputter().outputString(ele.getChild("sparql",Constants.HTTP_WWW_W3_ORG_2005_SPARQL_RESULTS));
			

			return new SPARQLResultoverXmpp(result,meta);
		}
	}
	
		
	
	public static class IQPacketFilter implements PacketFilter{
		public boolean accept(Packet arg0) {
			return arg0 instanceof SPARQLResultoverXmpp;
		}
	}
	
	public SparqlResult getSparqlResult(){
		SparqlResult result = new SparqlResult();
		result.setAnswer(this.result);
		result.setAnsweredBy(new SparqlEndpointIdentifier(SparqlEndpointType.P2P,this.getFrom()));
		
		
		return result;
		
		
		
	}
	


	
	

}
